import BaseView from "../../../Script/Base/BaseView";
import { EBundleType, ELotteryType, EResType, EViewType } from "../../../Script/Common/Enum";
import EventID from "../../../Script/Common/EventID";
import { GlobalData } from "../../../Script/Common/GlobalData";
import { IGameData, IMessage, IViewData } from "../../../Script/Interface/IMessage";
import AppService from "../../../Script/Service/AppService";
import Message from "../../Common/Message";
import { AdManager } from "../../naodong/ads/AdManager";
import { SceneGame } from "../../SceneGame";
import GameUtil from "../../Util/GameUtil";
import StorageUtil from "../../Util/StorageUtil";
import TownUI from "../town/TownUI";
import Eraser from "./Eraser";

const { ccclass, property } = cc._decorator;

const VIEW_DATA: IViewData = {
    name: 'LotteryUI',
    type: EViewType.Panel,
    bundle: EBundleType.Game,
    zIndex: 100,
};

@ccclass
export default class LotteryUI extends BaseView implements IMessage {
    public static instance: LotteryUI = null;

    @property({ type: cc.Node, tooltip: "弹窗" })
    private view: cc.Node = null;

    @property({ type: [cc.Node], tooltip: "弹窗节点" })
    private lottery: cc.Node[] = [];

    @property({ type: cc.Node, tooltip: "按钮父节点" })
    private btns: cc.Node = null;

    private gameData: IGameData = null;

    recvMsg(cmd: number, data: any) {
        switch (cmd) {
            case EventID.TEST_CMD:
                {

                }
                break;
        }
    }

    protected onLoad() {
        super.onLoad();
        this.addMessage(EventID.TEST_CMD, this);

        LotteryUI.instance = this;

        this.gameData = StorageUtil.instance.getGameData();

        this.initView();
    }

    private initView() {
        this.view.active = false;
        for (let i = 0; i < this.lottery.length; i++) {
            if (this.lottery[i]) {
                this.lottery[i].active = false;
            }
        }

        for (let i = 0; i < this.btns.children.length; i++) {
            const btn = this.btns.children[i];
            const config = GlobalData.getLotteryConfig(i);
            if (!config.isVideo) {
                const labMoney = cc.find('labMoney', btn).getComponent(cc.Label);
                labMoney.string = `${GameUtil.changeUnit(config.needMoney)}`;
            }
        }
    }

    public onCloseChild() {
        if (this.isHasChild) {
            this.isHasChild = false;
            this.initView();
        }
    }

    public onCloseClicked() {
        super.onCloseClicked();
        TownUI.instance.check();
    }

    private showLottery(i: ELotteryType) {
        GlobalData.lotteryType = i;
        this.isHasChild = true;
        this.view.active = true;
        this.lottery[i].active = true;
        this.lottery[i].getComponent(Eraser).restart();
    }

    private checkShowLottery(i: ELotteryType) {
        // 从第三个开始需要开启条件(智商)
        // 3=>60智商 4.65 5.70 6.75
        const brains = [0, 0, 60, 65, 70, 75];
        const brain = brains[i];
        if (i > 1 && this.gameData.brain < brain) {
            const text = `<color=#2A0302>需要</c><b><color=#ff2020>智商</c></b><color=#2A0302>达到${brain}哦~</color>`;
            SceneGame.instance.showToast(cc.v2(0, 0), text, true);
            return;
        }

        // 消耗货币或看视频进入
        const config = GlobalData.getLotteryConfig(i);
        if (!config.isVideo) {
            const need = config.needMoney;
            if (this.gameData.money >= need) {
                this.showLottery(i);
                GameUtil.instance.changeResValue(EResType.Money, -need);
                Message.instance.send(EventID.REFRESH_TOP_BAR, { type: EResType.Money, value: -need });
            } else {
                SceneGame.instance.showToast(cc.v2(0, 0), "资金不足~");
            }
        } else {
            // 播放视频
            AdManager.showVideoAd(() => {
                this.showLottery(i);
            });
        }
    }

    private onBtnsClicked(event: cc.Event.EventTouch, eventData: string) {
        // 神奇动物、勇闯金山、88就是发、瓜瓜乐、幸运四叶草、金银满堂
        const btnNames = ["btnAnimal", "btnMountain", "btnRich", "btnFruit", "btnGrass", "btnGold", "btnFree"];
        const name = event.target.name;
        for (let i = 0; i < btnNames.length; i++) {
            if (btnNames[i] == name) {
                this.checkShowLottery(i);
                break;
            }
        }
    }

    private onRestartClicked(event: cc.Event.EventTouch, eventData: string) {
        this.checkShowLottery(GlobalData.lotteryType);
    }
}
AppService.instance.registViewData(VIEW_DATA);